package Server;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import Util.XMLUtil;

public class Server extends JFrame {
	private static final long serialVersionUID = 1L;
	private JLabel label1, state, label2; 
	private JTextField port;
	//发送按钮
	private JButton start,fasong;
	private JPanel pInfo, pList,plast;//底部添加面板
	//添加文本域
	private JTextArea tlist,fasongTextArea;
	private Map<String, DeallingMessage> users = new HashMap<String, DeallingMessage>();	

	public Map<String, DeallingMessage> getUsers() {
		return users;
	}

	public Server(String name) {
		super(name);
		this.init();
	}

	public void init() {
		label1 = new JLabel();
		state = new JLabel();
		label2 = new JLabel();
		port = new JTextField();
		//创建一个2行15列的文本区域
		fasongTextArea = new JTextArea(2,25);
		//设置文本区的换行策略。
		fasongTextArea.setLineWrap(true);
		//设置换行方式（如果文本区要换行）。
		fasongTextArea.setWrapStyleWord (true);
		//设置选项卡要扩大到的字符数。
		fasongTextArea.setTabSize(9);
		
		
		
		port.addKeyListener(new KeyListener() {

			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				int keycode = e.getKeyChar();
				if (keycode < 48 || keycode > 57) {
					e.consume();
				}
				if(((JTextField)e.getSource()).getText().length()>3){
					e.consume();
				}
			}

			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub

			}

			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		start = new JButton();
		//添加发送按钮
		fasong = new JButton();
		fasong.setText("发送");
		fasong.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent a) {
				// TODO Auto-generated method stub
				try {
					//获取线程对象
					Collection<DeallingMessage> collection = Server.this.users.values();
					for (DeallingMessage cM : collection) {
						cM.sendMessage(XMLUtil.constructServerTextAreaXML("服务器信息："+fasongTextArea.getText()));
					}
				} catch (Exception e2) {
					e2.printStackTrace();
				} 
				fasongTextArea.setText(null);
			}
			
		});
		
		
		pInfo = new JPanel();
		pList = new JPanel();
		//创建面板
		plast = new JPanel();
		
		label1.setText("服务器状态");
		state.setText("停止");
		state.setForeground(Color.red);
		label2.setText("端口号");
		start.setText("启动服务器");
		start.setToolTipText("Click here to start the Server");
		start.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
			    Server.this.excute();
			}
		});
		port.setColumns(10);
		port.setToolTipText("Please Input The Target Port Number Here");
		port.setText("");
		pInfo.setBorder(BorderFactory.createTitledBorder("服务器信息"));
		pList.setBorder(BorderFactory.createTitledBorder("在线用户"));
		pInfo.add(label1);
		pInfo.add(state);
		pInfo.add(label2);
		pInfo.add(port);
		pInfo.add(start);
		//
		plast.add(fasong);
		plast.add(fasongTextArea);
		
		tlist = new JTextArea();
		tlist.setEditable(false);
		tlist.setForeground(Color.blue);
		tlist.setRows(20);
		tlist.setColumns(30);
		JScrollPane jScrollpane = new JScrollPane();
		jScrollpane.setViewportView(tlist);
		pList.add(jScrollpane);
		this.getContentPane().add(pInfo, BorderLayout.NORTH);
		this.getContentPane().add(pList, BorderLayout.CENTER);
		//将面板添加到底部
		this.getContentPane().add(plast, BorderLayout.SOUTH);
		
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.pack();
		this.setAlwaysOnTop(true);
		this.setResizable(false);
		this.setVisible(true);
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				try {
					Collection<DeallingMessage> collection = Server.this.users.values();
					for (DeallingMessage cM : collection) {
						cM.sendMessage(XMLUtil.constructServerClosingXML());
					}
				} catch (Exception e1) {
					e1.printStackTrace();
				} finally {
					//退出程序
					System.exit(0);
				}
			}
		});
	}

	public JLabel getCurState() {
		return state;
	}

	public JButton getStart() {
		return start;
	}

	public JTextArea getTlist() {
		return tlist;
	}

	private void excute() {
		if (Server.this.port.getText().equals("")) {
			JOptionPane.showMessageDialog(this, "请先输入端口号进行监听", "无法启动服务器",
					JOptionPane.WARNING_MESSAGE);
		} else {
			int port = Integer.parseInt(Server.this.port.getText());
			new ServerConnection(this, port).start();
		}
	}

	public static void main(String args[]) {
		new Server("服务器");
	}
}
